package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.xuexifenleiBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.StudyGuoDu_Adapter;
import com.oscar.ehome.view.pageview.view.StudyguoduView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/12/11
 */

public class Study_GuoDuActivity extends BasesActivity<StudyguoduView, PagePresenter> implements StudyguoduView ,XRefreshView.XRefreshViewListener,
        AdapterView.OnItemClickListener,ProgressFrameLayout.AgainRequestListener{
    PagePresenter presenter;
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.study_listview)
    MyList studyListview;
    @BindView(R.id.message_refview)
    XRefreshView messageRefview;
    @BindView(R.id.study_fragment)
    ProgressFrameLayout studyFragment;
    String  token="";
    public static long lastRefreshTime;
    StudyGuoDu_Adapter adapter;
    private int page = 1;//页数
    int size = 0;
    List<xuexifenleiBean.wenjian> learnList = new ArrayList<>();
    List<xuexifenleiBean.wenjian> learnListgengduo = new ArrayList<>();

    private int page_size = 20;//每页的条数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studyguodulist);
        ButterKnife.bind(this);
        // studylayout_title.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(StudyActivity.this).getSystemConfig().getColor()));
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(Study_GuoDuActivity.this).getSystemConfig().getColor()));
        initView();
        initData();
        onClick();
        //getMessage();


    }
    /**
     * 初始化
     */
    public void initView() {
        titleImgBack.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        studyListview.setVisibility(View.VISIBLE);
        titleImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleTopbar.setText("学习");
        token = SystemConfigFactory.getInstance(Study_GuoDuActivity.this).getSystemConfig().getToken();

    }
    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            studyFragment.stopRefresh();
            studyListview.setVisibility(View.VISIBLE);
           // presenter.getStudyList(token, "20", "1", serachStudyEdtext.getText().toString());
            presenter.gettudyGuoDu(token,"20", "1");
            showLoading();
        } else {
            studyListview.setVisibility(View.GONE);
            studyFragment.setVisibility(View.VISIBLE);
            studyFragment.setLoadFail();
        }

    }

    /**
     * 点击
     */
    public void onClick() {

        studyListview.setOnItemClickListener(this);

        studyFragment.setAgainRequestListener(this);
        messageRefview.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        messageRefview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        messageRefview.setPullLoadEnable(false);
        // 设置上次刷新的时间
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        messageRefview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        messageRefview.setAutoRefresh(false);
    }
    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(Study_GuoDuActivity.this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }



    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    @Override
    public void getxuexiguodulist(xuexifenleiBean result) {

        dismissLoading();
        messageRefview.stopRefresh();
        if (result != null) {
            if (page == 1) {
                learnList.clear(); // 将原有的内容清空
                learnList = result.getList();
                size = learnList.size();
            }else {
                learnListgengduo = result.getList();
                size = learnListgengduo.size();
                for (int i = 0; i < learnListgengduo.size(); i++) {
                    learnList.add(learnListgengduo.get(i));
                }
            }
            if (size > 0) {
                studyListview.setVisibility(View.VISIBLE);

                studyFragment.stopRefresh();
                if (size < page_size ) {
                    messageRefview.stopLoadMore();
                    messageRefview.setPullLoadEnable(false);
                }else{
                    messageRefview.setPullLoadEnable(true);
                }
                Log.e("123", "getxuexiguodulist: "+"789"+learnList.get(0).getImage());
                adapter = new StudyGuoDu_Adapter(Study_GuoDuActivity.this, learnList);
                studyListview.setAdapter(adapter);
                adapter.setLists(learnList);

            } else {
                studyListview.setVisibility(View.GONE);
                studyFragment.setVisibility(View.VISIBLE);
                studyFragment.setNoResult();
            }
        } else {
            studyListview.setVisibility(View.GONE);
            studyFragment.setNoResult();
        }

    }

    @Override
    public void erroy() {
        dismissLoading();
        messageRefview.stopRefresh();
        ToastUtil.show(Study_GuoDuActivity.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        messageRefview.stopRefresh();
        Toast.makeText(Study_GuoDuActivity.this, result, Toast.LENGTH_SHORT).show();

        studyFragment.setNoResult();

        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(Study_GuoDuActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(Study_GuoDuActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent in=new Intent();
        in.setClass(Study_GuoDuActivity.this,StudyActivity.class);
        in.putExtra("id",learnList.get(position).getId());
        startActivity(in);
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(Study_GuoDuActivity.this, R.string.pull_to_refresh_network_error);
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = messageRefview.getLastRefreshTime();
        messageRefview.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        presenter.gettudyGuoDu(token, "20", s);
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);
        presenter.gettudyGuoDu(token, "20", s);
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
